const fp = require('lodash/fp')

const cars = [
    {name:'BMW',horsepower:660,dollar_value:700000,in_stock:true},
    {name:'BYD',horsepower:650,dollar_value:648000,in_stock:false},
    {name:'BENZ',horsepower:550,dollar_value:132000,in_stock:false},
    {name:'VOLVO',horsepower:525,dollar_value:114200,in_stock:false},
    {name:'TOYOTA',horsepower:750,dollar_value:1850000,in_stock:true},
    {name:'AUDI',horsepower:700,dollar_value:1300000,in_stock:false},
]
// 练习一
let isLastInStock = fp.flowRight(fp.prop('in_stock'),fp.last)
console.log('f(cars): ', isLastInStock(cars));  // 执行结果：f(cars):  false

// 练习二
let f = fp.flowRight(fp.prop('name'),fp.first)
console.log('获取第一个car的name值',f(cars)); // 执行结果 '获取第一个car的name值' BMW

// 练习三
let _average = function(xs){
    return fp.reduce(fp.add,0,xs) / xs.length
}
// let averageDollarValue = function(cars){
//     let dollar_values = fp.map(function(car){
//         return car.dollar_value
//     },cars)
//     return _average(dollar_values)
// }

let averageDollarValue = fp.flowRight(_average,fp.map(car => car.dollar_value))
console.log('练习三答案',averageDollarValue(cars)); // 练习三答案 790700
// 练习四
/* 
    参数 字符串数组
    返回值字符串数组 空格转为下划线

*/
let _underscore = fp.replace(/\W+/g,'_')
let sanitizeNames = fp.flowRight(_underscore,fp.toLower,fp.first)
console.log('练习四结果',sanitizeNames(['Hello World'])); // 练习四结果 ["hello_world"]